/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.container.rest;

import com.alien.admin.annotation.Log;
import com.alien.admin.modules.container.domain.K8sPodMetricStatus;
import com.alien.admin.modules.container.service.K8sPodMetricStatusService;
import com.alien.admin.modules.container.service.K8sProjectService;
import com.alien.admin.modules.container.service.dto.K8sPodMetricStatusQueryCriteria;
import com.alien.admin.modules.container.service.dto.K8sProjectChartDto;
import com.alien.admin.modules.container.service.dto.K8sProjectPodStatusDto;
import com.alien.admin.modules.container.service.dto.K8sProjectQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zhouseyi
* @date 2021-10-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "集群资源使用管理")
@RequestMapping("/api/k8sPodMetricStatus")
public class K8sPodMetricStatusController {

    private final K8sPodMetricStatusService k8sPodMetricStatusService;
    private final K8sProjectService k8sProjectService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public void download(HttpServletResponse response, K8sPodMetricStatusQueryCriteria criteria) throws IOException {
        k8sPodMetricStatusService.download(k8sPodMetricStatusService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询集群资源使用")
    @ApiOperation("查询集群资源使用")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public ResponseEntity<Object> query(K8sPodMetricStatusQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(k8sPodMetricStatusService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增集群资源使用")
    @ApiOperation("新增集群资源使用")
    @PreAuthorize("@el.check('k8sPodMetricStatus:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody K8sPodMetricStatus resources){
        return new ResponseEntity<>(k8sPodMetricStatusService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改集群资源使用")
    @ApiOperation("修改集群资源使用")
    @PreAuthorize("@el.check('k8sPodMetricStatus:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody K8sPodMetricStatus resources){
        k8sPodMetricStatusService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除集群资源使用")
    @ApiOperation("删除集群资源使用")
    @PreAuthorize("@el.check('k8sPodMetricStatus:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        k8sPodMetricStatusService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/queryK8sProjectPodStatusList")
    @Log("查询pod节点列表")
    @ApiOperation("查询pod节点列表")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public ResponseEntity<Object> queryK8sProjectPodStatusList(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        List<K8sProjectPodStatusDto> data = k8sProjectService.queryK8sProjectPodStatusList(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }

    @PostMapping("/queryPodCpuHistoryChartInfo")
    @Log("查询pod节点cpu使用统计图表数据")
    @ApiOperation("查询pod节点cpu使用统计图表数据")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public ResponseEntity<Object> queryPodCpuHistoryChartInfo(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        K8sProjectChartDto data = k8sProjectService.queryPodCpuHistoryChartInfo(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }

    @PostMapping("/queryPodMemHistoryChartInfo")
    @Log("查询pod节点内存使用统计图表数据")
    @ApiOperation("查询pod节点内存使用统计图表数据")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public ResponseEntity<Object> queryPodMemHistoryChartInfo(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        K8sProjectChartDto data = k8sProjectService.queryPodMemHistoryChartInfo(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }

    @PostMapping("/queryK8sCpuHistoryChartInfo")
    @Log("查询集群cpu使用统计图表数据")
    @ApiOperation("查询集群cpu使用统计图表数据")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public ResponseEntity<Object> queryK8sCpuHistoryChartInfo(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        K8sProjectChartDto data = k8sProjectService.queryK8sCpuHistoryChartInfo(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }

    @PostMapping("/queryK8sMemHistoryChartInfo")
    @Log("查询集群内存使用统计图表数据")
    @ApiOperation("查询集群内存使用统计图表数据")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public ResponseEntity<Object> queryK8sMemHistoryChartInfo(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        K8sProjectChartDto data = k8sProjectService.queryK8sMemHistoryChartInfo(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }

    @PostMapping("/queryPodMetric")
    @Log("查询pod资源使用情况")
    @ApiOperation("查询pod资源使用情况")
    @PreAuthorize("@el.check('k8sPodMetricStatus:list')")
    public ResponseEntity<Object> queryPodMetric(@Validated @RequestBody K8sProjectQueryCriteria criteria){
        K8sPodMetricStatus data = k8sPodMetricStatusService.queryPodMetric(criteria);
        return new ResponseEntity<>(data,HttpStatus.OK);
    }
}
